/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xos_http.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../data_struct/head.h"
#include "../config/head.h"
#include "../macro/head.h"
#include "../impl/head.h"
#include "../msg/head.h"
#include "xos_heart.h"

namespace xos_http
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    xos_heart::xos_heart() : m_pProp(0)
    {
        m_pStr = mgr::container()->box()->string();
    }

    xos_heart::~xos_heart()
    {
        xos_stl::release_interface( m_pProp );
        xos_stl::release_interface( m_pStr );
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int xos_heart::helper_proc()
    {
        int ret = 0;

        xos::i_dir * pDir = mgr::container()->xos()->dir();
        xos::i_misc * pMisc = mgr::container()->misc();
        int nNum = 0;
        {
            nNum = m_pProp->size();
            m_pProp->reset();
        }
        for( int i = 0; i < nNum; ++i )
        {
            xos_box::i_variant * pV = 0;
            xos::i_file * pFile = 0;
            const char * pName = 0;
            module::T * pOldM = 0;
            module::T * pM = 0;
            file * pFindF, *pF = 0;
            int64_t cTime = 0;
            int64_t aTime = 0;
            int64_t mTime = 0;
            int nRet = 0;
            if( 0 == nRet )
            {
                pV = m_pProp->next( &pName, 0, 0, 0 );
            }
            if( 0 == nRet )
            {
                pFile = (xos::i_file *)pV->obj( 0 );
                xos_stl::init_pool_item( pM );
            }
            if( 0 == nRet )
            {
                pFile->get_file_time( &cTime, &aTime, &mTime );
                pM->m_mTime = mTime;
                m_pStr->set( m_hot_xos_path.c_str() );
                pMisc->path_append( m_pStr, pName );
                pM->m_full_file = m_pStr->get(0,0);
                pM->m_file = pName;
            }
            if( 0 == nRet )
            {
                pM->split_file_ext();
                if( pM->m_ext != config::get()->m_xos )
                {
                    nRet = 1;
                }
            }
            if( 0 == nRet )
            {
                int r = pM->load_servlet();
                if( 0 != r )
                {
                    nRet = 2;
                }
            }
            if( 0 == nRet )
            {
                http::i_servlet_mgr * pMgr = pM->m_pServletMgr;
                pM->m_uri_path = pMgr->path();
                pM->m_full_uri = pMgr->uri();
                pM->m_id = pMgr->id();
                pM->close_servlet();
            }
            if( 0 == nRet )
            {
                pFindF = mgr::get()->get_file_or_add_or_create_if_not_find( pM->m_full_uri.c_str(), 0, false );
                if( pFindF )
                {
                    pOldM = pFindF->get_module();
                    if( pOldM->m_mTime >= pM->m_mTime )
                    {
                        nRet = 3;
                    }
                    else
                    {
                        LOG4( "current xos is old, need replace" );
                    }
                }
                else
                {
                    LOG4( "has not this xos file, new added" ); 
                    xos_stl::init_pool_item( pF );
                    pF->set_module( pM );
                    pM->find_and_create_dir_by_uri_path();
                }
            }
            if( 0 == nRet )
            {
                pM->m_file = pM->m_id;
                pM->split_file_ext();
                pM->fmt_file_name();
            }
            if( 0 == nRet )
            {
                m_pStr->set( m_web_path.c_str() );
                m_pStr->url_append( pM->m_uri_path.c_str() );
                m_pStr->url_append( pM->m_file.c_str() );
                pM->m_full_path = m_pStr->get( 0, 0 );
            }
            if( 0 == nRet )
            {
                mgr::container()->common_misc()->copy( pM->m_full_file.c_str(), pM->m_full_path.c_str() );
                pDir->set_file_time( pM->m_full_path.c_str(), cTime, aTime, mTime );
                pM->m_full_file = pM->m_full_path;
            }
            if( 0 == nRet )
            {
                int r = pM->load_servlet();
                if( 0 != r )
                {
                    nRet = 4;
                }
            }
            if( 0 == nRet )
            {
                if( !pFindF )
                {
                    pF->m_pFatherPath->add_file( pM->m_id.c_str(), pF );
                    pF->m_pFatherPath->sort_file();
                    mgr::get()->get_file_or_add_or_create_if_not_find( pM->m_full_uri.c_str(), &pF, false );
                    pF = 0;
                }
                else
                {
                    pFindF->set_module( pM );
                }
            }
            xos_stl::release_interface( pF );
            xos_stl::release_ref( pOldM );
            xos_stl::release_ref( pM );
        }

        xos_stl::release_interface( pDir );

        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // 
    // proc
    // 
    int xos_heart::proc()
    {
        int ret = 0;

        if( 0 == ret )
        {
            xos_box::i_string * pStr = mgr::container()->box()->string();
            m_exe_path = mgr::container()->xos()->exe_path();
            {
                pStr->set( m_exe_path.c_str() );
                mgr::container()->misc()->path_append( pStr, config::get()->m_hot_replace_xos.c_str() );
                m_hot_xos_path = pStr->get( 0, 0 );
            }
            {
                pStr->set( m_exe_path.c_str() );
                mgr::container()->misc()->path_append( pStr, config::get()->m_web_root.c_str() );
                m_web_path = pStr->get( 0, 0 );
            }
            xos_stl::release_interface( pStr );
        }

        if( 0 == ret )
        {
            xos::i_dir * pDir = mgr::container()->xos()->dir();
            ret = pDir->scan_dir_and_files( xos::i_dir::FILE_TYPE_FILE, "xos", m_hot_xos_path.c_str(), &m_pProp, 1);
            xos_stl::release_interface( pDir );
        }

        if( 0 == ret )
        {
            ret = helper_proc();
        }

        return ret;
    }

} // xos_http
